package com.jpcc.vw.onlineRadio.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.RemoteException;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cns.android.atmospherelamp.AtmosphereLampManager;
/*Start by menglingqiang*/
import android.util.Log;
import java.util.Date;
/*End by menglingqiang*/
import com.jpcc.vw.onlineRadio.R;
import com.jpcc.vw.onlineRadio.adapter.AlbumCategoryAdapter;
import com.jpcc.vw.onlineRadio.constants.Constants;
import com.jpcc.vw.onlineRadio.model.IRadio;
import com.jpcc.vw.onlineRadio.model.bean.AlbumCategoryBean;
import com.jpcc.vw.onlineRadio.model.bean.AlbumCategoryEntity;
import com.jpcc.vw.onlineRadio.model.bean.BaseBean;
import com.jpcc.vw.onlineRadio.model.impl.RadioImpl;
import com.jpcc.vw.onlineRadio.utils.CommonSubscriber;
import com.jpcc.vw.onlineRadio.utils.ToastUtils;
import com.orhanobut.logger.Logger;
/*Start by menglingqiang*/
import com.jpcc.vw.tracelog.TracelogInterface;
import com.jpcc.vw.onlineRadio.application.MusicApplication;
/*End by menglingqiang*/

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action0;

/**
 * album分类画面
 */
public class AlbumCategoryActivity extends com.jpcc.vw.onlineRadio.base.BaseActivity {

    @BindView(R.id.recyclerview_category)
    RecyclerView recyclerview_category;
    @BindView(R.id.layout_back)
    RelativeLayout layout_back;
    @BindView(R.id.layout_search)
    RelativeLayout layout_search;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_show_empty)
    TextView tv_show_empty;
    @BindView(R.id.imv_top_bottom_line)
    ImageView imv_top_bottom_line;
    @BindView(R.id.progress_circular)
    ProgressBar progress_circular;
    @BindView(R.id.imv_new_search)
    ImageView imv_new_search;

    private AlbumCategoryAdapter adapter;
    private List<AlbumCategoryEntity> datas = new ArrayList<>();

    private IRadio radioModel;

    @Override
    public int getLayoutId() {
        return R.layout.activity_album_categorys;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        radioModel = new RadioImpl();
        layout_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlbumCategoryActivity.this.finish();
            }
        });
//        for (int index=0;index<10;index++){
//            AlbumCategoryEntity entity=new AlbumCategoryEntity();
//            entity.setName(""+index);
//            datas.add(entity);
//        }
        adapter = new AlbumCategoryAdapter(this, datas);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setItemPrefetchEnabled(false);
//        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerview_category.setLayoutManager(manager);
        recyclerview_category.setAdapter(adapter);
        imv_new_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AlbumCategoryActivity.this, SearchActivity.class);
                startActivity(intent);
            }
        });
        tv_title.setText(getResources().getString(R.string.string_20));
    }

    @Override
    public void doAtmosphere(boolean rgbOn, String color) {
        Logger.i("doAtmosphere" + "rgbOn-->" + rgbOn);
        Logger.i("doAtmosphere" + "color-->" + color);
        if (rgbOn) {
            if (TextUtils.isEmpty(color)) {
                imv_top_bottom_line.setColorFilter(Color.TRANSPARENT);
            } else {
                imv_top_bottom_line.setColorFilter(Color.parseColor(color));
            }
        } else {
            imv_top_bottom_line.setColorFilter(Color.TRANSPARENT);
        }
    }

    @Override
    public void setupData() {
        requestAlbumCategory();
        if (Constants.isRelease) {
            try {
                boolean atmosphereStatus = AtmosphereLampManager.getInstance(this).isHmiAtmosphereLampOn();
                String color = AtmosphereLampManager.getInstance(this).getHmiAtmosphereLampColor();
                doAtmosphere(atmosphereStatus, color);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    private void requestAlbumCategory() {
	/*Start by menglingqiang*/
        Date dt = new Date();
        try {
            TracelogInterface inter = MusicApplication.getLogInterface();
            if(inter != null){
                inter.sendLogData(dt.toString(),dt.toString(),AlbumCategoryActivity.this.getClass().getName(),"DEBUG",null,"GetAlbumCategory request");
            }else {
                Log.e("jgj","inter == null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*End by menglingqiang*/
        radioModel.GetAlbumCategory().doOnSubscribe(new Action0() {
            @Override
            public void call() {
//                showProgress();
                progress_circular.setVisibility(View.VISIBLE);
            }
        }).subscribe(new CommonSubscriber<AlbumCategoryBean>() {
            @Override
            public void onSuccess(AlbumCategoryBean response) {
//                hideProgress();
                progress_circular.setVisibility(View.INVISIBLE);
                if (Constants.STATUS_SUCCESS_CODE == response.getCode() && response.getData() != null && response.getData().size() > 0) {
                    tv_show_empty.setVisibility(View.GONE);
                    adapter.setDatas(response.getData());
                }

                /*Start by menglingqiang*/
                Date dt = new Date();
                try {
                    TracelogInterface inter = MusicApplication.getLogInterface();
                    if(inter != null){
                        inter.sendLogData(dt.toString(),dt.toString(),AlbumCategoryActivity.this.getClass().getName(),"DEBUG",response.getStatusCode().toString(),"GetAlbumCategory onSuccess");
                    }else {
                        Log.e("jgj","inter == null");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                /*End by menglingqiang*/
            }

            @Override
            public void onFailure(BaseBean response) {
//                hideProgress();
                progress_circular.setVisibility(View.INVISIBLE);
                ToastUtils.show(response.getStatusMessage());
                tv_show_empty.setVisibility(View.VISIBLE);

                /*Start by menglingqiang*/
                Date dt = new Date();
                try {
                    TracelogInterface inter = MusicApplication.getLogInterface();
                    if(inter != null){
                        inter.sendLogData(dt.toString(),dt.toString(),AlbumCategoryActivity.this.getClass().getName(),"ERROR","400","GetAlbumCategory onFailure");
                    }else {
                        Log.e("jgj","inter == null");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                /*End by menglingqiang*/
            }
        });
    }
}
